﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MediaDataModel;

namespace MediaClient.Publish
{
    public partial class VideoOutputForm : BaseMainForm
    {
        #region private properties

        private CCTV6Entities db = null;

        private long mediaPackID;

        private string discBarcode = null;

        private Point firstCompanyName = new Point(6, 14);
        private Point firstDate = new Point(308, 14);
        private Point firstSpliter = new Point(5, 33);
        private int spliterWith = 485;
        private int spliterRowHeigh = 30;
        #endregion

        public VideoOutputForm()
        {
            InitializeComponent();
            this.mediaPackID = 1;
        }

        public VideoOutputForm(long mediaPackID)
            : this()
        {
            this.mediaPackID = mediaPackID;
        }

        #region Events

        private void PublishForm_Load(object sender, EventArgs e)
        {
            if (this.mediaPackID == 0)
                this.mediaPackID = 1;
            dgvMediaPack.AutoGenerateColumns = false;
            this.db = new CCTV6Entities();
            InitializeForm();
        }

        private void dgvMediaPack_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

            MediaPack selectedMediaPack = dgvMediaPack.Rows[e.RowIndex].DataBoundItem as MediaPack;
            if (selectedMediaPack == null || selectedMediaPack.ID == this.mediaPackID)
                return;
            this.mediaPackID = selectedMediaPack.ID;
            DisPlayMetaData(selectedMediaPack);

        }

        private void btnNext_MouseMove(object sender, MouseEventArgs e)
        {
            this.btnNext.BackgroundImage = global::MediaClient.Properties.Resources.下一步_1;
        }

        private void btnNext_MouseLeave(object sender, EventArgs e)
        {
            this.btnNext.BackgroundImage = global::MediaClient.Properties.Resources.下一步;
        }

        private void lblControl_Click(object sender, EventArgs e)
        {
            Label lblCurrent = sender as Label;
            if (lblCurrent != null)
            {
                short publishFlag = 0;
                switch (lblCurrent.Name)
                {
                    case "lblSDIPublish":
                        publishFlag = 1;
                        break;
                    case "lblFilePublish":
                        publishFlag = 2;
                        break;
                    case "lblDiscPublish":
                        publishFlag = 3;
                        break;
                }
                if (publishFlag != 0)
                {
                    PublishSettingForm publishForm = new PublishSettingForm(this.mediaPackID,publishFlag);
                    publishForm.ShowDialog();
                }
            }
        }

        #endregion

        #region private methods

        private void InitializeForm()
        {
            //pnlPublishInfo.Controls.Clear();
            //if (this.mediaPackID != 0)
            //{
            //    var mediaPack = db.MediaPacks.Where(media => media.ID == this.mediaPackID).FirstOrDefault();
            //    if (mediaPack != null)
            //    {
            //        this.lblMediaPack1.Name = mediaPack.Name;
            //        this.discBarcode = mediaPack.DiscBarcode;
            //        dgvMediaPack.DataSource = db.MediaPacks.Where(media => media.DiscBarcode == this.discBarcode);
            //        dgvMediaPack.ClearSelection();
                    
            //        DisPlayMetaData(mediaPack);
            //        DisplayPublishInfo(this.mediaPackID);
            //    }
                
            //}
        }

        private void DisPlayMetaData(MediaPack oMediaPack)
        {
            pnlMediaInfo.Controls.Clear();
            //pnlPublishInfo.Controls.Clear();
            lblMediaPack1.Text = oMediaPack.Name;
            int index = 0;
            int startX = 4;
            int startY = 2;
            short addCount = 21;
            short multHeight = 100;
            int height = lblMediaPackName.Height;
            int width = lblMediaPackName.Width;
            int currentY = startY + index * addCount;
            foreach (var mediaMetaData in oMediaPack.MetaDataRecords.OrderBy(record => record.MetaDataID))
            {
                var metaData = mediaMetaData.MetaDataName;
                if (metaData != null)
                {
                    Label lblMetaDataName = new Label();
                    lblMetaDataName.Location = new Point(startX,currentY);
                    lblMetaDataName.AutoSize = false;
                    lblMetaDataName.Size = lblMediaPackName.Size;
                    lblMetaDataName.Font = lblMediaPackName.Font;
                    lblMetaDataName.ForeColor = lblMediaPackName.ForeColor;
                    lblMetaDataName.Text = metaData.Name;
                    lblMetaDataName.TextAlign = ContentAlignment.MiddleRight;
                    lblMetaDataName.BackColor = Color.Transparent;
                    pnlMediaInfo.Controls.Add(lblMetaDataName);

                    if (metaData.DataType !=2 )
                    {
                        Label lblValue = new Label();
                        lblValue.Location = new Point(startX + 92, currentY);
                        lblValue.AutoSize = true;
                        currentY = currentY + addCount;
                        lblValue.Font = lblMediaPack1.Font;
                        lblValue.ForeColor = lblMediaPack1.ForeColor;
                        lblValue.Text = mediaMetaData.MetaDataContent;
                        //lblValue.BorderStyle = BorderStyle.Fixed3D;
                        lblValue.TextAlign = ContentAlignment.MiddleLeft;
                        pnlMediaInfo.Controls.Add(lblValue);
                    }
                    else
                    {
                        TextBox txtValue = new TextBox();
                        txtValue.Text = mediaMetaData.MetaDataContent;
                        txtValue.Multiline = true;
                        txtValue.ScrollBars = ScrollBars.Vertical;
                        txtValue.Size = new Size(314, multHeight);
                        //txtValue.BackColor = ;
                        txtValue.Location = new Point(startX + 92, currentY);
                        currentY = multHeight + currentY + addCount;
                        txtValue.BorderStyle = BorderStyle.None;
                        txtValue.Font = lblMediaPack1.Font;
                        txtValue.ForeColor = lblMediaPack1.ForeColor;
                        
                        pnlMediaInfo.Controls.Add(txtValue);

                    }
                    
                }

            }
            DisplayPublishInfo(oMediaPack.ID);
        }

        private void DisplayPublishInfo(long mediaPackID)
        {
            //pnlPublishInfo.Controls.Clear();
            //using(CCTV6Entities db = new CCTV6Entities())
            //{
            //    var infos = db.PublishInfors.Where(item => item.MediaPackID == mediaPackID).ToList();
            //    int i = 0;
            //    foreach (var item in infos)
            //    {
            //        Label lblCompanyName = new Label();
            //        lblCompanyName.Location = new Point(firstCompanyName.X, firstCompanyName.Y + i * spliterRowHeigh);
            //        lblCompanyName.AutoSize = true;
            //        lblCompanyName.Text = item.Receiver.CompanyName;
            //        pnlPublishInfo.Controls.Add(lblCompanyName);

            //        Label lblDate = new Label();
            //        lblDate.Text = "【" + item.StartDate.Value.ToString("yyyy-MM-dd") + " 至 " + item.EndDate.Value.ToString("yyyy-MM-dd") + "】";
            //        lblDate.Location = new Point(firstDate.X, firstDate.Y + i * spliterRowHeigh);
            //        lblDate.AutoSize = true;
            //        pnlPublishInfo.Controls.Add(lblDate);

            //        Label lblSpliter = new Label();
            //        lblSpliter.Location = new Point(firstSpliter.X, firstSpliter.Y + i * spliterRowHeigh);
            //        lblSpliter.AutoSize = false;
            //        lblSpliter.Size = new Size(485, 1);
            //        lblSpliter.BackColor = Color.LightGray;
            //        pnlPublishInfo.Controls.Add(lblSpliter);
            //        i++;
            //    }
            //}
        }

        #endregion

        private void btnNext_Click(object sender, EventArgs e)
        {

        }
    }
}
